package com.pureut.order.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pureut.order.domain.OrderSupplierManagement;
import com.pureut.order.domain.PullDown;
import com.pureut.order.dto.CustomerManagementDto;
import com.pureut.order.dto.OrderSupplierManagementDto;
import com.pureut.order.dto.OrderSupplierManagementMqDto;
import com.pureut.order.export.OrderSupplierManagementExport;
import com.pureut.order.export.OrderSupplierManagementimportExport;
import com.pureut.order.mapper.OrderSupplierManagementMapper;
import com.pureut.order.mapper.PurchaseOrderMapper;
import com.pureut.order.service.OrderSupplierManagementService;
import com.pureut.order.util.VariousMqUtil;
import com.pureut.order.vo.OrderReviewVo;
import com.pureut.order.vo.OrderSupplierManagementVo;
import com.sale.common.core.domain.R;
import com.sale.common.core.exception.GlobalException;
import com.sale.common.dict.utils.DictUtils;
import com.sale.common.mybatis.core.page.PageQuery;
import com.sale.common.mybatis.core.page.TableDataInfo;
import com.sale.common.redis.utils.RedisUtils;
import com.sale.common.satoken.utils.LoginHelper;
import com.sale.order.api.dto.CustomerBalance;
import com.sale.system.api.RemoteCodeService;
import com.sale.system.api.RemoteReviewService;
import com.sale.system.api.RemoteStaffManagementService;
import com.sale.system.api.domain.SysDictData;
import com.sale.system.api.domain.dto.SysStaffManagementDto;
import com.sale.system.api.domain.vo.SysAuditHistoryVo;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 供应商实现层
 */
@RequiredArgsConstructor
@Service
public class OrderSupplierManagementServiceImpl implements OrderSupplierManagementService {


    private final OrderSupplierManagementMapper orderSupplierManagementMapper;


    private final PurchaseOrderMapper purchaseOrderMapper;

    @DubboReference
    RemoteStaffManagementService remoteStaffManagementService;

    @DubboReference
    private final RemoteCodeService remoteCodeService;

    @DubboReference
    private final RemoteReviewService remoteReviewService;

    /**
     * 供应商分页查询
     *
     * @param orderSupplierManagementVo
     * @return
     */
    @Override
    public TableDataInfo<OrderSupplierManagementDto> getList(OrderSupplierManagementVo orderSupplierManagementVo, PageQuery pageQuery) {
        Page<OrderSupplierManagementDto> allList = orderSupplierManagementMapper.getAllList(pageQuery.build(), orderSupplierManagementVo);
        List<OrderSupplierManagementDto> records = allList.getRecords();
        for (OrderSupplierManagementDto entity : records) {
            if (entity.getAscriptionBy() != null) {
                //归属人
                if (!"0".equals(entity.getAscriptionBy()) && entity.getAscriptionBy() != null) {
                    Long staffId = Long.valueOf(entity.getAscriptionBy());
                    R<SysStaffManagementDto> info = remoteStaffManagementService.getInfo(staffId);
                    if (info.getData() != null) {
                        entity.setAscriptionByDict(info.getData().getStaffName());
                    }
                }
            }
        }
        return TableDataInfo.build(allList);
    }

    /**
     * 新增供应商
     *
     * @param orderSupplierManagementVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addSupplier(OrderSupplierManagementVo orderSupplierManagementVo) throws Exception {
        //判断简称是否存在
        OrderSupplierManagement orderSupplierManagementOne = orderSupplierManagementMapper.selectOne(new QueryWrapper<OrderSupplierManagement>().lambda().eq(OrderSupplierManagement::getSupplierAbbreviation, orderSupplierManagementVo.getSupplierAbbreviation()));
        if (orderSupplierManagementOne != null) {
            throw new GlobalException("该供应商简称已经存在");
        }

        OrderSupplierManagement orderSupplierManagement = new OrderSupplierManagement();
        orderSupplierManagement.setDeptId(LoginHelper.getDeptId());
        orderSupplierManagement.setSupplierName(orderSupplierManagementVo.getSupplierName());
        orderSupplierManagement.setSupplierAbbreviation(orderSupplierManagementVo.getSupplierAbbreviation());
        String authorityCoding = remoteCodeService.authorityCoding("order:purchase:supplier:list");
        if (authorityCoding == null) {
            throw new GlobalException("未进行单据配置,单据编码生成失败");
        }
        orderSupplierManagement.setSupplierNum(authorityCoding);
        orderSupplierManagement.setCreateBy(LoginHelper.getUsername());
        orderSupplierManagement.setContactsBy(orderSupplierManagementVo.getContactsBy());
        orderSupplierManagement.setContactsInformation(orderSupplierManagementVo.getContactsInformation());
        orderSupplierManagement.setSupplierAddress(orderSupplierManagementVo.getSupplierAddress());
        orderSupplierManagement.setInvoicingInformation(orderSupplierManagementVo.getInvoicingInformation());
        orderSupplierManagement.setBusinessLicense(orderSupplierManagementVo.getBusinessLicense());
        orderSupplierManagement.setAuditStatus(1);
        orderSupplierManagement.setCreateTime(new Date());
        orderSupplierManagement.setRemark(orderSupplierManagementVo.getRemark());
        orderSupplierManagement.setSupplierCategoryCode(orderSupplierManagementVo.getSupplierCategoryName());
        orderSupplierManagement.setRatepayerNumber(orderSupplierManagementVo.getRatepayerNumber());
        orderSupplierManagement.setContactsPhone(orderSupplierManagementVo.getContactsPhone());
        orderSupplierManagement.setBankDeposit(orderSupplierManagementVo.getBankDeposit());
        orderSupplierManagement.setOpenAccount(orderSupplierManagementVo.getOpenAccount());
        if (!("".equals(orderSupplierManagementVo.getAscriptionBy()))) {
            orderSupplierManagement.setAscriptionBy(Long.parseLong(orderSupplierManagementVo.getAscriptionBy()));
        }
        int insert = orderSupplierManagementMapper.insert(orderSupplierManagement);
        //mq发送消息
        OrderSupplierManagementMqDto orderSupplierManagementMqDto = new OrderSupplierManagementMqDto();
        BeanUtils.copyProperties(orderSupplierManagement, orderSupplierManagementMqDto);
        //租户id
        String userIdStr = LoginHelper.getUserIdStr();
        String tenantId = RedisUtils.getCacheObject(userIdStr);
        orderSupplierManagementMqDto.setTenantId(Long.parseLong(tenantId));
        orderSupplierManagementMqDto.setId(orderSupplierManagement.getId());
        VariousMqUtil.orderSupplierManagementMqSend(orderSupplierManagementMqDto);
        return insert > 0;
    }

    /**
     * 冻结供应商
     */
    @Override
    public boolean frozenSupplier(Long id) {
        //获取供应商信息
        OrderSupplierManagement orderSupplierManagement = orderSupplierManagementMapper.selectById(id);
        if (orderSupplierManagement.getAuditStatus() == 4) {
            orderSupplierManagement.setAuditStatus(6);
            return orderSupplierManagementMapper.updateById(orderSupplierManagement) > 0;
        } else {
            throw new GlobalException("只能冻结启用的供应商");
        }

    }

    /**
     * 解冻供应商
     *
     * @param id
     * @return
     */
    @Override
    public boolean thawSupplier(Long id) {
        //获取供应商信息
        OrderSupplierManagement orderSupplierManagement = orderSupplierManagementMapper.selectById(id);
        if (orderSupplierManagement.getAuditStatus() == 6) {
            orderSupplierManagement.setAuditStatus(4);
            return orderSupplierManagementMapper.updateById(orderSupplierManagement) > 0;
        } else {
            throw new GlobalException("只能解冻已冻结的供应商");
        }

    }

    /**
     * 撤销审核
     *
     * @param id
     * @return
     */
    @Override
    public boolean revokeApproval(Long id) {
        //获取供应商信息
        OrderSupplierManagement orderSupplierManagement = orderSupplierManagementMapper.selectById(id);
        if (orderSupplierManagement.getAuditStatus() == 1) {
            orderSupplierManagement.setAuditStatus(2);

//            //删除审核代办消息提醒
//            feignService.deleteDoc(orderSupplierManagement.getSupplierNum(),"供应商管理");

            return orderSupplierManagementMapper.updateById(orderSupplierManagement) > 0;
        } else {
            throw new GlobalException("只能撤销待审核的数据");
        }

    }

    /**
     * 获取导出数据列表
     *
     * @param orderSupplierManagementVo
     * @return
     */
    @Override
    public List<OrderSupplierManagementExport> getInfoExport(OrderSupplierManagementVo orderSupplierManagementVo) {
        List<OrderSupplierManagementExport> exportList;
//        //获取供应商状态缓存字典数据
//        List<SysDictData> statusArray = DictUtils.getDictCache("order_supplier_status");
//        Map<String, String> statusMap = statusArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
//
//        //获取供应商等级缓存字典数据
//        List<SysDictData> levelArray = DictUtils.getDictCache("order_supplier_level");
//        Map<String, String> levelMap = levelArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        if (orderSupplierManagementVo.getIdStr() == null) {
            exportList = orderSupplierManagementMapper.getExportList(orderSupplierManagementVo);
        } else {
            List<String> idList = Arrays.asList(orderSupplierManagementVo.getIdStr().split(","));
            exportList = orderSupplierManagementMapper.getDataByIdStr(idList);
        }
        for (OrderSupplierManagementExport entity : exportList) {
//            //状态
//            entity.setAuditStatus(statusMap.get(entity.getAuditStatus()));
//            //等级
//            entity.setSupplierLevel(levelMap.get(entity.getSupplierLevel()));
            if (entity.getAscriptionBy() != null) {
                //归属人
                String ascriptionBy = entity.getAscriptionBy();
//                entity.setAscriptionBy(feignService.getUserName(Long.valueOf(ascriptionBy)));
            }
        }
        return exportList;
    }

    /**
     * 导入
     *
     * @param coCustomer
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String importMouldData(List<OrderSupplierManagementimportExport> coCustomer) {
        if (coCustomer.size() == 0) {
            throw new GlobalException("导入供应商信息不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        //获取供应商等级缓存字典数据
        List<SysDictData> levelArray = DictUtils.getDictCache("order_supplier_level");
        Map<String, String> levelMap = levelArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        for (OrderSupplierManagementimportExport mouldModelExport : coCustomer) {
            OrderSupplierManagement orderSupplierManagement = new OrderSupplierManagement();
            try {
                if (mouldModelExport.getSupplierName() == null || mouldModelExport.getSupplierAbbreviation() == null || mouldModelExport.getSupplierLevel() == null) {
                    throw new GlobalException("存在必填字段为空，请核实");
                }
                // 赋值
                orderSupplierManagement.setSupplierName(mouldModelExport.getSupplierName());
                orderSupplierManagement.setContactsBy(mouldModelExport.getContactsBy());
                orderSupplierManagement.setContactsInformation(mouldModelExport.getContactsInformation());
                orderSupplierManagement.setSupplierAddress(mouldModelExport.getSupplierAddress());
                //判断简称是否唯一
                OrderSupplierManagement orderSupplierManagementOne = orderSupplierManagementMapper.selectOne(new QueryWrapper<OrderSupplierManagement>().lambda().eq(OrderSupplierManagement::getSupplierAbbreviation, mouldModelExport.getSupplierAbbreviation()));
                if (orderSupplierManagementOne != null) {
                    throw new GlobalException("该供应商简称已经存在");
                }
                orderSupplierManagement.setSupplierAbbreviation(mouldModelExport.getSupplierAbbreviation());
                //供应商等级
                for (Map.Entry<String, String> entry : levelMap.entrySet()) {
                    if (entry.getValue().equals(mouldModelExport.getSupplierLevel())) {
                        orderSupplierManagement.setSupplierLevel(Integer.parseInt(entry.getKey()));
                        break;
                    }
                }
                orderSupplierManagement.setCreateBy(LoginHelper.getUsername());
                orderSupplierManagement.setAuditStatus(1);
                //获取供应商编码
                String authorityCoding = remoteCodeService.authorityCoding("order:supplier:list");
                if (authorityCoding == null) {
                    throw new GlobalException("未进行单据配置,单据编码生成失败");
                }
                orderSupplierManagement.setSupplierNum(authorityCoding);
                orderSupplierManagement.setCreateTime(new Date());
                //判断归属人是否存在

                String userName = mouldModelExport.getAscriptionBy();
//                if (userName != null) {
//                    AjaxResult byUserName = feignService.getByUserName(userName);
//                    if (byUserName == null) {
//                        throw new GlobalException("未获取到相关归属人");
//                    }
//                    SysUser data = JSON.parseObject(JSON.toJSONString(byUserName.get("data")), SysUser.class);
//                    orderSupplierManagement.setAscriptionBy(data.getUserId());
//                }
                orderSupplierManagement.setDeptId(LoginHelper.getDeptId());
                orderSupplierManagementMapper.insert(orderSupplierManagement);

//                //mq发送消息
//                OrderSupplierManagementMqDto orderSupplierManagementMqDto = new OrderSupplierManagementMqDto();
//                BeanUtils.copyProperties(orderSupplierManagement, orderSupplierManagementMqDto);
//                VariousMqUtil.orderSupplierManagementMqSend(orderSupplierManagementMqDto);

                successNum++;
                successMsg.append(successNum + "、供应商信息 " + mouldModelExport.getSupplierName() + " 导入成功");

            } catch (Exception e) {
                failureNum++;
                String msg = failureNum + "、供应商 " + mouldModelExport.getSupplierName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new GlobalException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }

        return successMsg.toString();
    }

    /**
     * 删除供应商
     *
     * @param ids
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteSupplierByIds(String ids) throws Exception {
        List<OrderSupplierManagement> orderSupplierManagementList = new ArrayList<>();
        if (ids != null) {
            List<String> stringIds = Arrays.asList(ids.split(","));
            //获取供应商列表
            List<OrderSupplierManagement> orderSupplierManagements = orderSupplierManagementMapper.selectBatchIds(stringIds);

            for (OrderSupplierManagement entity : orderSupplierManagements) {
                if (entity.getAuditStatus() == 1) {
                    throw new GlobalException("数据启用中，不允许删除");
                }
//                List<PurchaseOrder> purchaseOrders = purchaseOrderMapper.selectList(new QueryWrapper<PurchaseOrder>().lambda().eq(PurchaseOrder::getSupplierId, entity.getId()));
                //如果没有关联数据，并且状态为已冻结，就放入删除列表
//                boolean delData = 6 == entity.getAuditStatus() && purchaseOrders.size() == 0;
//                if (delData || entity.getAuditStatus() == 2 || entity.getAuditStatus() == 5) {
                orderSupplierManagementList.add(entity);

//                    feignService.deleteDoc(entity.getSupplierNum(),"供应商管理");

//                }
            }

//            if (orderSupplierManagementList.size() > 0) {
//                StringBuilder stringBuilder = new StringBuilder();
//                for (OrderSupplierManagement entity : orderSupplierManagementList) {
//                    stringBuilder.append(entity.getSupplierNum()).append(",");
//                }
//                StringBuilder stringBuilder1 = stringBuilder.deleteCharAt(stringBuilder.length() - 1);
//                //mq同步删除加工商
//                OrderSupplierManagementMqDto orderSupplierManagementMqDto = new OrderSupplierManagementMqDto();
//                orderSupplierManagementMqDto.setSupplierDeleteMark(12);
//                orderSupplierManagementMqDto.setSupplierNum(stringBuilder1.toString());
//                VariousMqUtil.orderSupplierManagementMqSendDelete(orderSupplierManagementMqDto);
        } else {
            throw new GlobalException("请至少勾选一条数据");
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (OrderSupplierManagement entity : orderSupplierManagementList) {
            stringBuilder.append(entity.getId()).append(",");
        }
        StringBuilder stringBuilder1 = stringBuilder.deleteCharAt(stringBuilder.length() - 1);
        //mq同步删除加工商
        OrderSupplierManagementMqDto orderSupplierManagementMqDto = new OrderSupplierManagementMqDto();
        orderSupplierManagementMqDto.setIdStr(stringBuilder1.toString());
        VariousMqUtil.orderSupplierManagementMqSendDelete(orderSupplierManagementMqDto);
        return orderSupplierManagementMapper.deleteBatchIds(orderSupplierManagementList) > 0;

    }

    /**
     * 查看供应商信息
     *
     * @param id
     * @return
     */
    @Override
    public OrderSupplierManagementDto getSupplierById(Long id) {
        OrderSupplierManagementDto supplierById = orderSupplierManagementMapper.getSupplierById(id);
        if (supplierById.getAscriptionBy() != null && !"0".equals(supplierById.getAscriptionBy())) {
            //归属人
            Long staffId = Long.valueOf(supplierById.getAscriptionBy());
            R<SysStaffManagementDto> info = remoteStaffManagementService.getInfo(staffId);
            if (info.getData() != null) {
                supplierById.setAscriptionByDict(info.getData().getStaffName());
            }
        }
//        //获取供应商所属机构缓存字典数据
//        List<SysDictData> organizationArray = DictUtils.getDictCache("order_affiliated_organization");
//        Map<String, String> organizationMap = organizationArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
//        orderSupplierManagementDto.setAffiliatedOrganizationDict(organizationMap.get(String.valueOf(orderSupplierManagement.getAffiliatedOrganization())));
//        orderSupplierManagementDto.setAffiliatedOrganization(String.valueOf(orderSupplierManagement.getAffiliatedOrganization()));
//        //获取供应商等级缓存字典数据
//        List<SysDictData> levelArray = DictUtils.getDictCache("order_supplier_level");
//        Map<String, String> levelMap = levelArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
//        orderSupplierManagementDto.setSupplierLevelDict(levelMap.get(String.valueOf(orderSupplierManagement.getSupplierLevel())));
//        orderSupplierManagementDto.setSupplierLevel(String.valueOf(orderSupplierManagement.getSupplierLevel()));
//        //归属人
//        Long userId = orderSupplierManagement.getAscriptionBy();
//        String userName = feignService.getUserName(userId);
//        orderSupplierManagementDto.setAscriptionBy(String.valueOf(userId));
//        orderSupplierManagementDto.setAscriptionByDict(userName);
        return supplierById;
    }


    /**
     * 根据供应商编码查询供应商详情信息
     *
     * @param code
     * @return
     */
    @Override
    public OrderSupplierManagementDto getSupplierByNum(String code) {
        OrderSupplierManagementDto supplierByNum = orderSupplierManagementMapper.selectByCode(code);
//        if (supplierByNum.getAscriptionBy() != null) {
//            //归属人
//            Long staffId = Long.valueOf(supplierByNum.getAscriptionBy());
//            R<SysStaffManagementDto> info = remoteStaffManagementService.getInfo(staffId);
//            if (info.getData() != null) {
//                supplierByNum.setAscriptionByDict(info.getData().getStaffName());
//            }
//        }
//
        return supplierByNum;
    }


    /**
     * 提交审核
     *
     * @param id
     * @return
     */
    @Override
    public boolean reivewSubmit(Long id) {
        OrderSupplierManagement orderSupplierManagement = orderSupplierManagementMapper.selectById(id);
        if (orderSupplierManagement.getAuditStatus() == 2) {
            orderSupplierManagement.setAuditStatus(1);

//            //新增审核代办
//            SysWorkbenchAuditAgentVo sysWorkbenchAuditAgentVo = new SysWorkbenchAuditAgentVo();
//
//            sysWorkbenchAuditAgentVo.setReceiptName("供应商管理");
//            sysWorkbenchAuditAgentVo.setDocConfig("order:purchase:supplier:examine");
//            sysWorkbenchAuditAgentVo.setReceiptNumber(orderSupplierManagement.getSupplierNum());//供应商编号
//            sysWorkbenchAuditAgentVo.setStatus(1);//待审核
//            sysWorkbenchAuditAgentVo.setReceiptContent("有单据待审核,请及时审核");
//
//            feignService.insertAuditAgent(sysWorkbenchAuditAgentVo);

            return orderSupplierManagementMapper.updateById(orderSupplierManagement) > 0;
        } else {
            throw new GlobalException("只能操作待提交的数据");
        }

    }

    /**
     * 修改供应商
     *
     * @param orderSupplierManagementVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateSupplierById(OrderSupplierManagementVo orderSupplierManagementVo) throws Exception {
        OrderSupplierManagement orderSupplierManagement = orderSupplierManagementMapper.selectById(orderSupplierManagementVo.getId());
        BeanUtils.copyProperties(orderSupplierManagementVo, orderSupplierManagement);
        orderSupplierManagement.setAscriptionBy(Long.parseLong(orderSupplierManagementVo.getAscriptionBy()));
        //mq发送消息
        OrderSupplierManagementMqDto orderSupplierManagementMqDto = new OrderSupplierManagementMqDto();
        BeanUtils.copyProperties(orderSupplierManagement, orderSupplierManagementMqDto);
        VariousMqUtil.orderSupplierManagementMqSend(orderSupplierManagementMqDto);
        return orderSupplierManagementMapper.updateById(orderSupplierManagement) > 0;
    }

    /**
     * 供应商审核
     *
     * @param orderReviewVo
     * @return
     */
    @Override
    public boolean supplierReview(OrderReviewVo orderReviewVo) throws Exception {

        OrderSupplierManagement orderSupplierManagement = orderSupplierManagementMapper.selectById(orderReviewVo.getId());
        if (orderSupplierManagement.getAuditStatus() == 1 || orderSupplierManagement.getAuditStatus() == 3) {
            String perms = "order:purchase:supplier:examine";
            //获取菜单id
            int isAdopt = orderReviewVo.getIsAdopt();
            String auditDoc = orderSupplierManagement.getSupplierNum();
            String auditRemarks = orderReviewVo.getReviewComments();
            Long userId = LoginHelper.getUserId();
            SysAuditHistoryVo sysAuditHistoryVo = new SysAuditHistoryVo();
            sysAuditHistoryVo.setPerms(perms);
            sysAuditHistoryVo.setAuditType(2);
            sysAuditHistoryVo.setAuditState(isAdopt);
            sysAuditHistoryVo.setAuditRemarks(auditRemarks);
            sysAuditHistoryVo.setAuditDoc(auditDoc);
            sysAuditHistoryVo.setUserId(userId);
            int data = remoteReviewService.getSequence(sysAuditHistoryVo);
            if (data == 0) {
                throw new GlobalException("未对该审核进行配置");
            }
            //1:通过，2：审核中，3：没权限 4:该人已经审核过
            if (orderReviewVo.getIsAdopt() == 1) {
                if (data == 1) {
                    orderSupplierManagement.setAuditStatus(4);
//                    //修改审核代办状态为已处理
//                    feignService.updateStatus(3, auditDoc, "供应商管理");

                    return orderSupplierManagementMapper.updateById(orderSupplierManagement) > 0;
                } else if (data == 2) {
                    orderSupplierManagement.setAuditStatus(3);
                    return orderSupplierManagementMapper.updateById(orderSupplierManagement) > 0;
                } else if (data == 3) {
                    throw new GlobalException("没有审核权限");
                } else {
                    throw new GlobalException("该用户已经审核过");
                }
            } else {
                if (data == 1 || data == 2) {
                    orderSupplierManagement.setAuditStatus(5);

//                    //新增审核代办
//                    SysWorkbenchAuditAgentVo sysWorkbenchAuditAgentVo = new SysWorkbenchAuditAgentVo();
//
//                    sysWorkbenchAuditAgentVo.setReceiptName("供应商管理");
//                    sysWorkbenchAuditAgentVo.setDocConfig("order:purchase:supplier:examine");
//                    sysWorkbenchAuditAgentVo.setReceiptNumber(orderSupplierManagement.getSupplierNum());//供应商编号
//                    sysWorkbenchAuditAgentVo.setStatus(2);//待处理
//                    sysWorkbenchAuditAgentVo.setPassFlag("1");
//                    sysWorkbenchAuditAgentVo.setCreateName(orderSupplierManagement.getCreateBy());
//                    sysWorkbenchAuditAgentVo.setReceiptContent("单据审核未通过,请及时查看");
//
//                    feignService.insertAuditAgent(sysWorkbenchAuditAgentVo);

                    return orderSupplierManagementMapper.updateById(orderSupplierManagement) > 0;
                } else if (data == 3) {
                    throw new GlobalException("没有审核权限");
                } else {
                    throw new GlobalException("该用户已经审核过");
                }
            }
        } else {
            throw new GlobalException("只能对待审核和审核中的数据进行处理");
        }
    }

    /**
     * 供应商启用状态修改
     *
     * @param orderSupplierManagementVo
     * @return
     */
    @Override
    public boolean updateStatus(OrderSupplierManagementVo orderSupplierManagementVo) {
        OrderSupplierManagement orderSupplierManagement = orderSupplierManagementMapper.selectById(orderSupplierManagementVo.getId());
        orderSupplierManagement.setAuditStatus(Integer.parseInt(orderSupplierManagementVo.getAuditStatus()));
        return orderSupplierManagementMapper.updateById(orderSupplierManagement) > 0;
    }

    /**
     * 获取开启的供应商
     *
     * @return
     */
    @Override
    public List<HashMap<String, String>> getOpenSupplier() {
        List<HashMap<String, String>> resultMap = new ArrayList<>();
        List<OrderSupplierManagementDto> openSupplierList = orderSupplierManagementMapper.getOpenSupplierList();
        for (OrderSupplierManagementDto entity : openSupplierList) {
            HashMap<String, String> map = new HashMap<>();
            map.put("label", entity.getSupplierName());
            map.put("value", entity.getSupplierNum());
            resultMap.add(map);
        }
        return resultMap;
    }

    /**
     * 获取开启的供应商(财务模块调用)
     *
     * @return
     */
    @Override
    public Map<String, String> getOpenSupplierFinance() {
        List<OrderSupplierManagementDto> openSupplierList = orderSupplierManagementMapper.getOpenSupplierList();


        Map<String, String> collect = openSupplierList.stream()
            .collect(Collectors.toMap(OrderSupplierManagementDto::getSupplierNum, OrderSupplierManagementDto::getSupplierName));
        return collect;
    }

    /**
     * 根据编码获取供应商信息
     *
     * @param code
     * @return
     */
    @Override
    public OrderSupplierManagementDto getSupplierByCode(String code) {

        return orderSupplierManagementMapper.selectByCode(code);
    }

    /**
     * 通过供应商名称获取供应商编码(财务模块使用)
     */
    @Override
    public String getCodeBySupplierName(String supplierName) {
        return orderSupplierManagementMapper.getCodeBySupplierName(supplierName);
    }

    /**
     * 供应商下拉
     */
    @Override
    public List<PullDown> supplierPullDown() {
        return orderSupplierManagementMapper.supplierPullDown();
    }

    /**
     * 更新供应商余额(财务模块使用)
     */
    @Override
    public boolean updateSupplierBalance(CustomerBalance customer) {
        return orderSupplierManagementMapper.updateSupplierBalance(customer);
    }
}
